<?php
namespace app\common\model;
use app\common\model\BaseModel;
use think\Db;
use chrome\ChromePhp;//引入chromephp插件
use common\Common;//公共函数

use UtilQRcode\UtilQRcode;//引入二维码类

use mergeImg\MergeImg;//引入图片合成类

/**
 * 提供用户推广二维码服务
 * @name LibUserCode.php
 * @package njwl
 * @category model
 * @link http://www.chanekeji.com
 * @author cd
 * @version 5.0
 * @copyright CHANGE INC
 * @since 2019-07-25
 */
class LibUserCode extends BaseModel{
    private $m_code;
    
	/**
	 * 构造函数
	 */
    function __construct(){
		$this->m_code = db('base_user_code');
        parent::__construct();
    }
    
	/**
	 * 添加用户推广二维码
	 * @param array $condition 用户id
	 * @param string $targetPath 格式 upload/image/store/headImage/ 存储路径
	 * @param string $bgUrl 背景图
	 * @return array
	 */
	function getCode($condition,$targetPath,$bgUrl){
		try{
		    $codeResult = $this->m_code->where($condition)->find();
		    if(!$codeResult){
		        $addId = $this->m_code->insertGetId($condition);
		        if($addId == true){
		            $codeUrl = UtilQRcode::getParamQRcode($addId,2);//携带二维码id
		            //修改推广二维码
					$this->m_code->where(array('id' => $addId))->update(array('code' => $codeUrl));
					$targetPathName = $this->getContainQrcode($condition,$targetPath,$bgUrl);
					
		            return  Common::errorArray(0, "添加成功", $targetPathName['data']);
		        }else{
		            return  Common::errorArray(1, "添加失败", $codeUrl);
		        }
		    }else{
		        return Common::errorArray(0, "存在二维码", $codeResult['contain_qrcode']);
		    }
		}catch (Exception $ex){
		    $this->errorLog(__CLASS__, __FUNCTION__, $ex);
		    return  Common::errorArray(1, "数据库操作失败",$ex);
		}
	}
	
	/**
	 * 保存合成的推广二维码
	 * @param array $condition
	 * @param string $targetPath 格式 upload/image/store/headImage/ 存储路径
	 * @param string $bgUrl 背景图
	 * @return array $result
	 */
	function getContainQrcode($condition,$targetPath,$bgUrl){
	    $codeResult = $this->findCode($condition);
	    try{
	        if(empty($codeResult['data']['contain_qrcode'])){//不存在组合图：生成组合图
	            $targetPathName = MergeImg::getTargetPath($targetPath);
	            $result = MergeImg::mergeContainJpgImg($codeResult['data']['code'],$bgUrl,array('x' => 170, 'y' => 570),null, array('x'=>500, 'y' => 500),$targetPathName);
	           	if($result){
			    	$resultUpdate = $this->m_code->where($condition)->update(array('contain_qrcode' => $targetPathName)); //持久化路径到数据库
		            if($resultUpdate){
		                return Common::errorArray(0, "添加成功", $targetPathName);
		            }else{
		                return Common::errorArray(1, "添加失败", $result);
		            }
			    }else{
			    	return Common::errorArray(1, "生成图片", $result);
			    }
	        }else{
	            return Common::errorArray(0, "存在合成二维码", $codeResult['data']['contain_qrcode']);
	        }
	    } catch (Exception $ex) {
	        $this->errorLog(__CLASS__, __FUNCTION__, $ex);
	        return  Common::errorArray(1, "数据库操作失败",$ex);
	    }
	}

	/**
	 * 查找推广二维码
	 * @param array $condition
	 * @return array
	 */
	function findCode($condition){
		try{
			$result = $this->m_code->where($condition)->find();
			if($result == true){
				return Common::errorArray(0, "查找成功", $result);
			}else{
				return Common::errorArray(1, "查找为空", $result);
			}
		}catch (Exception $ex){
		    $this->errorLog(__CLASS__, __FUNCTION__, $ex);
		    return Common::errorArray(1, "数据库操作失败",$ex);
		}
	}
	
	
	//------------------------微信小程序码的合成与分享功能------------------------------------
	
	/**
	 * 合成推广二维码
	 * @param array $codeInfo 条件
	 * @param string $targetPath 格式 upload/image/store/headImage/ 存储路径
	 * @param string $wxappCode 小程序码路径
	 * @param string $backgroundImg 背景图路径
	 * @return array
	 */
	function getAppCode($codeInfo,$targetPath,$wxappCode,$backgroundImg){
	    try{
	        $codeResult = $this->m_code->where($codeInfo)->find();
	        if(!$codeResult || empty($codeResult['contain_qrcode'])){ //不存在app码则重新合成
	            $row = $codeInfo;
	            $row['code'] = $wxappCode;
	            $addId = $this->m_code->insertGetId($row);
	            if($addId == true){
	                $this->m_code->where( array('id'=>$addId) )->update($row);
	                $targetPathName = mergeImg::getTargetPath($targetPath);
	                // $result = mergeImg::mergeContainJpgImg($wxappCode, $backgroundImg, array('x' => 80, 'y' =>460), '', array('x'=>260, 'y' => 260), $targetPathName);
	                $resultUpdate = $this->m_code->where($codeInfo)->update( array('contain_qrcode' => $targetPathName) );//持久化路径到数据库
	                return  Common::errorArray(0, "添加成功", $targetPathName);
	            }else{
	                return  Common::errorArray(1, "添加失败", $resultUpdate);
	            }
	        }else{
	            return Common::errorArray(0, "存在二维码", $codeResult['contain_qrcode']);
	        }
	    }catch (Exception $ex){
	        $this->errorLog(__CLASS__, __FUNCTION__, $ex);
	        return  Common::errorArray(1, "数据库操作失败",$ex);
	    }
	}
	
	
}